/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package task1;

import java.util.Arrays;
import java.util.List;



/**
 *
 * @author Admin
 */
public class MyTransfer {
    
    private static final List<Character> numlist = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    private static final String[] x1 = {"", "один ", "два ", "три ", "четыре ", "пять ", "шесть ", "семь ",
        "восемь ", "девять "};
    private static final String[] xx1 = {"", "одна ", "две "};
    private static final String[] x11 = {"десять ", "одиннадцать ", "двенадцать ", "тринадцать ", "четырнадцать ",
        "пятнадцать ", "шестнадцать ", "семнадцать ", "восемнадцать ", "девятнадцать "};
    private static final String[] x10 = {"", "", "двадцать ", "тридцать ", "сорок ", "пятьдесят ", "шестьдесят ",
        "семьдесят ", "восемьдесят ", "девяносто "};
    private static final String[] x100 = {"", "сто ", "двести ", "триста ", "четыреста ", "пятьсот ", "шестьсот ",
        "семьсот ", "восемьсот ", "девятьсот "};
    private static final String[] rank = {"", "тысяча ", "тысячи ", "тысяч ", "миллион ", "миллиона ", "миллионов ",
        "миллиард ", "миллиарда ", "миллиардов "};
    
    /**
     *
     */
    public static String changeString(char c, String[] s) {
        int j = Character.getNumericValue(c);
        return s[j];
    }
    
    public static String changeRank(char c, String[] s, int i) {
        int j = Character.getNumericValue(c);
        if(j == 1) {
            return s[i - 2];
        }
        else if(j == 2 | j == 3 | j == 4) {
            return s[i - 1];
        }
        else {
            return s[i];
        }
    }
    
    public static boolean checked(String s) {
        for(int i = 0; i < s.length(); i++) {
            if(!numlist.contains(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    
    
    
    public String transfer(String s){
        String temp = "";
        if(!checked(s)){
            return "Неверный формат числа";
        }
        String reverseString = new StringBuffer(s).reverse().toString();
        for(int i = reverseString.length() - 1; i >= 0; i--){
            switch (i){
                case 11:
                    temp += changeString(reverseString.charAt(i), x100);
                    break;
                case 10:
                    if(Character.getNumericValue(reverseString.charAt(i)) == 1) {
                        break;
                    }
                    temp += changeString(reverseString.charAt(i), x10);
                    break;
                case 9:
                    if(Character.getNumericValue(reverseString.charAt(i + 1)) == 1) {
                        temp += changeString(reverseString.charAt(i), x11);
                        temp += rank[i];
                        break;
                    }
                    else {
                        temp += changeString(reverseString.charAt(i), x1);
                    }
                    temp += changeRank(reverseString.charAt(i), rank, i);
                    break;
                case 8:
                    temp += changeString(reverseString.charAt(i), x100);
                    break;
                case 7:
                    if(Character.getNumericValue(reverseString.charAt(i)) == 1) {
                        break;
                    }
                    temp += changeString(reverseString.charAt(i), x10);
                    break;
                case 6:
                    if(Character.getNumericValue(reverseString.charAt(i + 1)) == 1) {
                        temp += changeString(reverseString.charAt(i), x11);
                        temp += rank[i];
                        break;
                    }
                    else {
                        temp += changeString(reverseString.charAt(i), x1);
                    }
                    temp += changeRank(reverseString.charAt(i), rank, i);
                    break;
                case 5:
                    temp += changeString(reverseString.charAt(i), x100);
                    break;
                case 4:
                    if(Character.getNumericValue(reverseString.charAt(i)) == 1) {
                        break;
                    }
                    temp += changeString(reverseString.charAt(i), x10);
                    break;
                case 3:
                    int j = Character.getNumericValue(reverseString.charAt(i));
                    if(Character.getNumericValue(reverseString.charAt(i + 1)) == 1) {
                        temp += changeString(reverseString.charAt(i), x11);
                        temp += rank[i];
                        break;
                    }
                    else if(j == 1 | j == 2) {
                        temp += changeString(reverseString.charAt(i), xx1);
                    }
                    else {
                        temp += changeString(reverseString.charAt(i), x1);
                    }
                    temp += changeRank(reverseString.charAt(i), rank, i);
                    break;
                case 2:
                    temp += changeString(reverseString.charAt(i), x100);
                    break;
                case 1:
                    if(Character.getNumericValue(reverseString.charAt(i)) == 1) {
                        break;
                    }
                    temp += changeString(reverseString.charAt(i), x10);
                    break;
                case 0:   
                    if(s.length() > 1){
                        if(Character.getNumericValue(reverseString.charAt(i + 1)) == 1) {
                        temp += changeString(reverseString.charAt(i), x11);
                        break;
                        }
                    }
                    temp += changeString(reverseString.charAt(i), x1);                    
                    break;
            }            
        }        
        return temp;
    }
}
 
    
